package com.cpi.cornpp.futurepurchase.form;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.struts.validator.ValidatorForm;

import com.cpi.cornpp.customer.view.CustomerView;
import com.cpi.cornpp.customerpurchase.view.CustomerPurchaseView;
import com.cpi.cornpp.futurepurchase.view.FuturePurchaseView;
import com.cpi.cornpp.period.exception.PeriodException;
import com.cpi.cornpp.period.service.PeriodService;
import com.cpi.cornpp.product.view.ProductView;
import com.cs.common.utils.StringUtils;

public class FuturePurchaseForm extends ValidatorForm {

	private static final long serialVersionUID = -6482968047340824115L;
	
	/**
	 * Principal componente (Header) Front del end View
	 */
	private FuturePurchaseView futurePurchaseView;
	
	/**
	 * view de cliente para realizar la busqueda de clientes
	 */
	private CustomerView customerViewSearch;
		
	/**
	 * view de producto para realizar la busqueda de productos
	 */
	private ProductView productViewSearch;
	
	/**
	 * Lista de clientes resultado de la busqueda de clientes
	 */
	private Collection resultCustomerViews;
	
	/**
	 * Lista de clientes seleccionados del resultado
	 */	
	private String[] selectedCustomerIds = {}; 

	/**
	 * Lista de clientes seleccionados asignados para ser removidos
	 */	
	private String[] removedCustomerIds = {}; 
	
	/**
	 * Atributo para identificar si se esta creando o editando un registro
	 */
	private boolean updateForm;

	/**
	 * Lista de periodos activos
	 */
	List arrLperiodService;
		
	/**
	 * Lista de los
	 */
	private ArrayList forwardYearList;

	private List resultProducts;
	
	
		
	public FuturePurchaseForm(){
		super();
		
		futurePurchaseView = new FuturePurchaseView();
		customerViewSearch = new CustomerView();
		//TODO leer desde el properties
		customerViewSearch.setDescription(StringUtils.EMPTY_STRING);
		resultCustomerViews = new ArrayList(0);
		forwardYearList = new ArrayList(0);
		
		productViewSearch = new ProductView();
		resultProducts = new ArrayList(0);		
	}
	/**
	 * Devuelve el View del Front end
	 * @return
	 */
	public FuturePurchaseView getFuturePurchaseView() {
		return futurePurchaseView;
	}

	/**
	 * Asignar un futurePurchaseView al Front end
	 * @param futurePurchaseView
	 */
	public void setFuturePurchaseView(FuturePurchaseView futurePurchaseView) {
		this.futurePurchaseView = futurePurchaseView;
	}
	
	/**
	 * Devuelve el Customerview utilizado para la busqueda de CustomerViews
	 * @return
	 */
	public CustomerView getCustomerViewSearch() {
		return customerViewSearch;
	}
	
	/**
	 * Asigna el Customerview utilizado para la busqueda de CustomerViews
	 * @param customerView
	 */
	public void setCustomerViewSearch(CustomerView customerView) {
		this.customerViewSearch = customerView;
	}
	
	public Collection getResultCustomerViews() {
		return resultCustomerViews;
	}
	public void setResultCustomerViews(Collection customerViews) {
		this.resultCustomerViews = customerViews;
	}
	public ArrayList getForwardYearList() {
		return forwardYearList;
	}
	public void setForwardYearList(ArrayList forwardYearList) {
		this.forwardYearList = forwardYearList;
	}

	public String[] getSelectedCustomerIds() { 
		  return this.selectedCustomerIds; 
	} 
	public void setSelectedCustomerIds(String[] selectedItems) { 
		  this.selectedCustomerIds = selectedItems; 
	}
	
	/**
	 * Devuelve la lista de checboxes seleccionados de la lista de clientes asignados.
	 * es decir esta es la lista de clientes a remover de la lista
	 * @return
	 */
	public String[] getRemovedCustomerIds() {
		return removedCustomerIds;
	}
	public void setRemovedCustomerIds(String[] removedCustomerIds) {
		this.removedCustomerIds = removedCustomerIds;
	}
	public ProductView getProductViewSearch() {
		return productViewSearch;
	}
	public void setProductViewSearch(ProductView productViewSearch) {
		this.productViewSearch = productViewSearch;
	}

	public int getTotalFixed(){
		int i = 0;
		List cps = this.futurePurchaseView.getCustomerPurchases();
		Iterator it = cps.iterator();
		while(it.hasNext()){
			CustomerPurchaseView cp = (CustomerPurchaseView)it.next();
			int j = cp.getQuantityFixed();
			if(j!=0)
				i = i + j;			
		}
		return i;
	}
	
	public int getTotalDirect(){
		int i = 0;
		List cps = this.futurePurchaseView.getCustomerPurchases();
		Iterator it = cps.iterator();
		while(it.hasNext()){
			CustomerPurchaseView cp = (CustomerPurchaseView)it.next();
			int j = cp.getQuantityDirect();
			if(j!=0)
				i = i + j;			
		}
		return i;
	}
	
	public int getTotalEarly(){
		int i = 0;
		int j = this.futurePurchaseView.getQuantity();
		if(j!=0)
			i = j - (getTotalDirect() + getTotalFixed());
		return i;
	}
	
	/**
	 * Comtemplar utilizar la implementacion del metodo reset
	 * del propio struts
	 *
	 */
	public void reset(){
		this.futurePurchaseView = new FuturePurchaseView();
		this.customerViewSearch = new CustomerView();
	}
	/**
	 * Obtiene la bandera de update/insert del form
	 * @return
	 */
	public boolean isUpdateForm() {
		return updateForm;
	}
	/**
	 * Asigna la bandera de update/insert del form
	 * @param updateForm
	 */
	public void setUpdateForm(boolean updateForm) {
		this.updateForm = updateForm;
	}
	
	/**
	 * Devuelve el resultado de productos encontrados
	 * @return
	 */
	public List getResultProducts() {
		return resultProducts;
	}
	
	/**
	 * Asigna el resultado de productos encontrados
	 * @param resultProducts
	 */
	public void setResultProducts(List resultProducts) {
		this.resultProducts = resultProducts;
	}
	/**
	 * Devuelve la lista de periodos activos
	 * @return
	 */
	public List getArrLperiodService() {		
		if(arrLperiodService==null){
			PeriodService periodService = null;
			periodService = new PeriodService();
			List _arrLperiodService = null;
			try {
				_arrLperiodService = periodService.getActivePeriods();
			} catch (PeriodException e) {			
				e.printStackTrace();
			}
			arrLperiodService = _arrLperiodService ;
		}
		return arrLperiodService;	
		
	}
	
	/**
	 * Asigna la lista de periodos activos 
	 * @param arrLperiodService
	 */
	public void setArrLperiodService(List arrLperiodService) {
		this.arrLperiodService = arrLperiodService;
	}
	
}
